package com.sdms.core;

import java.util.List;

import com.sdms.core.auth.IAuthoriseService;
import com.sdms.core.auth.UnauthorisedUserException;

/**
 * Secure Delivery Management System (SDMS) com.sdms.core User: Tharindu Munasinghe Mar 7, 2013 3:28:37 PM Copyright � 2013
 */
public class DeliveryManagementSystem implements IDeliveryManagementSystem
{
	IAuthoriseService authService;
	IDeliveryStore delStore;

	public DeliveryManagementSystem( IAuthoriseService authService )
	{
		this.authService = authService;
		this.delStore = DeliveryStore.getInstance();
	}

	@Override
	public DeliveryDetails viewDeliveryDetails( String sessionId, String itemId ) throws UnauthorisedUserException
	{
		authService.authoriseUser( sessionId, SupportedOperation.VIEW_DELIVERY );
		return delStore.viewDetails( itemId );
	}

	@Override
	public List<DeliveryDetails> viewDeliveryDetails( String sessionId ) throws UnauthorisedUserException
	{
		authService.authoriseUser( sessionId, SupportedOperation.VIEW_LIST );
		return delStore.viewDetails();
	}

	@Override
	public void updateDeliveryDetails( String sessionId, String itemId )
	{
		// TODO Auto-generated method stub

	}

	@Override
	public String addItemDelivery( String sessionId, String customerName, String collectFrom, String destination, String itemDesc ) throws UnauthorisedUserException
	{
		authService.authoriseUser( sessionId, SupportedOperation.ADD_DELIVERY );
		return delStore.addItem( createNewItem( customerName, collectFrom, destination, itemDesc ) );
	}

	@Override
	public DeliveryDetails cancelItemDelivery( String sessionId, String itemId ) throws UnauthorisedUserException
	{
		authService.authoriseUser( sessionId, SupportedOperation.CANCEL_DELIVERY );
		return delStore.removeItem( itemId );
	}

	@Override
	public void addNewUser( String sessionId, String userName )
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void removeUser( String sessionId, String userName )
	{
		// TODO Auto-generated method stub

	}

	private DeliveryDetails createNewItem( String customerName, String collectFrom, String destination, String itemDesc )
	{
		DeliveryDetails delDetails = new DeliveryDetails( customerName, collectFrom, destination, itemDesc );
		return delDetails;
	}

	@Override
	public DeliveryDetails addItemDeliveryAndView( String sessionId, String customerName, String collectFrom, String destination, String itemDesc ) throws UnauthorisedUserException
	{
		authService.authoriseUser( sessionId, SupportedOperation.ADD_DELIVERY );
		String itemId = delStore.addItem( createNewItem( customerName, collectFrom, destination, itemDesc ) );
		authService.clearExistingPreviousPriviledgesAddNewPriviledges( SupportedOperation.VIEW_DELIVERY );
		return delStore.viewDetails( itemId );
	}
}
